<?php
/**
 * Created by PhpStorm.
 * User: zouhua
 * Date: 2017/4/26
 * Time: 20:27
 */

namespace app\controllers;


use app\components\WebAuthController;
use app\extensions\weixin\WeiXin;
use app\models\Activity;
use app\models\Job;
use app\models\Region;
use yii\web\View;

class PublishController extends WebAuthController {

    public $layout = 'detail';

    public function beforeAction($action) {
        if (!parent::beforeAction($action)) {
            return FALSE;
        }
        if ($this->user->getIsVerify() || $this->user->getIsVerifyDeny()) {
            $this->layout = 'user';
            echo $this->render(['msg' => '您的账号还未通过审核！'], '/common/error');
            return FALSE;
        }
        return TRUE;
    }

    public function actionIndex() {
        $this->layout = 'user';
        $this->view->title = '我要发布';
        $user = $this->user;

        $items = [
            Activity::CID_SHA_LONG => ['name' => '讲座沙龙', 'style' => '#10B1CE', 'img' => 'ico-jzsl.png', 'type' => Activity::TYPE_ACTIVITY],
            Activity::CID_SAI_SHI => ['name' => '大学生赛事', 'style' => '#10CE96', 'img' => 'ico-dxsss.png', 'type' => Activity::TYPE_ACTIVITY],
            Activity::CID_ZHI_YUAN_ZHE => ['name' => '志愿者义工', 'style' => '#85D200', 'img' => 'ico-zyz.png', 'type' => Activity::TYPE_ACTIVITY],
        ];

        if (!$this->user->getIsStudent()) {
            $items[Activity::CID_JOB] = ['name' => '实习生', 'style' => '#600CD3', 'img' => 'ico-sxs.png', 'type' => Activity::TYPE_JOB];
            $items[Activity::CID_PRACTICE] = ['name' => '兼职', 'style' => '#D3126D', 'img' => 'ico-jz.png', 'type' => Activity::TYPE_JOB];
        }
        return $this->render(compact('user', 'items'));
    }

    public function actionSave() {
        $type = $this->getParam('type');
        $id = $this->getParam('id');
        $cid = $this->getParam('cid');

        if (!isset(Activity::$cidLabels[$cid]) || !isset(Activity::$typeLabels[$type])) {
            return $this->redirect(['user/publish']);
        }

        /* @var $model Activity */
        $model = $this->findModel($id, Activity::className());
        if ($model->isNewRecord) {
            $model->type = $type;
            $model->uid = $this->user->id;
            $model->cid = $cid;
        } elseif (!$model->getIsEdit($this->user)) {
            return $this->redirect(['publish/index']);
        }

        if ($model->start_time <= 0) {
            $model->start_time = $model->end_time = '';
        }

        $user = $this->user;
        if (!$model->load($this->post())) {
            $this->view->title = ($model->isNewRecord ? '发布' : '修改') . Activity::$cidLabels[$cid];
            $data = Region::getPickerData();
            $this->view->registerJs("window.region = {$data};", View::POS_BEGIN);
            return $this->render(compact('user', 'model'));
        }

        $thumbs = $this->upload();
        if (!empty($thumbs)) {
            $model->thumbs = $thumbs;
        }

        return $model->save();
    }

    protected function upload() {
        $thumbs = $this->getParam('original_image', []);

        $uploads = $this->getParam('uploads');
        if (!empty($uploads)) {
            /* @var $wx WeiXin */
            $wx = \Yii::$app->get('weixin');
            foreach ($uploads as $media_id) {
                $file = $wx->getFileForMediaId($media_id);
                if (NULL !== $file) {
                    $thumbs[] = $file;
                }
            }
        }
        return $thumbs;
    }
}